<template>
  <el-dialog :visible="settingsVisible" :title="myIndex > -1? '编辑'+titleName : '新增'+titleName" @close="closeDialog">
    <!-- 商品分类 -->
    <el-form v-if="settingMode === 'percent'" ref="percent" :rules="rule" :model="settingForm">
      <el-form-item :label-width="labeWidth" label="抽佣比例" prop="percent">
        <el-row>
          <el-col :span="11" >
            <el-input v-model="settingForm.percent" maxlength="20" placeholder="请输入0 - 99之间的正整数数字"/>
          </el-col>
          <el-col :span="2">
            <span>&nbsp;%</span>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item :label-width="labeWidth" label="有效期" prop="startTime">
        <el-row>
          <el-col :span="6" >
            <el-form-item prop="startTime">
              <el-date-picker
                :picker-options="pickerStartTime"
                v-model="settingForm.startTime"
                :clearable="false"
                style="width:150px"
                value-format="yyyy-MM"
                type="month"
                placeholder="选择开始时间"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item prop="endTime">
              <el-date-picker
                :picker-options="pickerEndTime"
                v-model="settingForm.endTime"
                :clearable="false"
                style="width:150px"
                value-format="yyyy-MM"
                type="month"
                placeholder="选择结束时间"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form-item>
      <div class="bottomBtn">
        <el-button v-preventReClick type="primary" @click="submitForm('percent')">确定</el-button>
        <el-button type="info" @click="closeDialog">取消</el-button>
      </div>
    </el-form>
  </el-dialog>
</template>

<script>
import { Message } from 'element-ui'
import { vipInfoAll } from '@/api/product'
// <sendGoods :send-goods-visible.sync="sendGoods" :send-goods-index="sendGoodsIndex" :send-goods-id="sendGoodsId"/>
export default {
  props: {
    settingsVisible: {
      type: Boolean,
      required: true
    },
    myIndex: {
      type: Number,
      required: true
    },
    dialogData: {
      type: Object,
      required: true
    },
    settingMode: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      labeWidth: '120px',
      settingForm: {},
      pickerStartTime: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e6
        }
      },
      pickerEndTime: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e6
          // console.log(this.settingForm.startTime)
          // alert(this.settingForm.startTime)
          // return time.getTime() < Date.now() - 8.64e6
        }
      },
      rule: {
        percent: [
          { required: true, message: '请填写抽佣比例', trigger: 'blur' },
          { pattern: /^([1-9]\d|\d)$/, message: '请输入0 - 99之间的正整数数字', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '请选择开始时间', trigger: 'blur' }
        ],
        endTime: [
          { required: true, message: '请选择结束时间', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    titleName() {
      const titleMap = {
        'percent': '抽佣比例'
      }
      return titleMap[this.settingMode]
    }
  },
  watch: {
    myIndex(val) {
      if (val > -1) {
        this.settingForm = Object.assign({}, this.dialogData) // this.objectCon({}, this.dialogData) //
        console.log(this.settingForm.percent > 0)
        if (this.settingForm.percent > 0) this.settingForm.percent = parseInt(this.settingForm.percent * 100)
      } else {
        this.settingForm = {}
      }
    }
  },
  mounted() {
    // if (this.settingMode === 'discount' || this.settingMode === 'membersRebate') {
    //   // this.getConstantPercent()
    //   this.getMomList()
    // }
  },
  methods: {
    // getMomList() {
    //   vipInfoAll().then(res => {
    //     if (res.code === '200') {
    //       this.momList = res.data
    //     } else {
    //       Message.error(res.message)
    //     }
    //   })
    // },
    submitForm(name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          if (new Date(this.settingForm.startTime).getTime() > new Date(this.settingForm.endTime).getTime()) {
            Message.error('开始时间必须小于结束时间')
            return false
          }
          // 将比例 例如 50 转为 后台接收的数据 0.5
          this.settingForm.percent = this.settingForm.percent / 100
          // 向父级插件冒一个‘updataParent’ 的方法, 并且把填好的数据带过去， 到父级去做处理
          this.$emit('updataParent', this.settingForm)
          this.settingForm = {}
          this.closeDialog()
        }
      })
    },
    closeDialog() {
      this.$emit('update:settingsVisible', false)
      this.$emit('update:myIndex', -1)
      this.$refs['percent'].clearValidate()
      this.settingForm = {}
    }
  }
}
</script>
<style scope>
.bottomBtn{
    height: 80px;
    padding-top: 20px;
    width:100%;
    text-align: center;
    border-top: 1px solid #ddd
}
.bottomBtn button{
    display: inline-block;
}
select{
    width:100%
}
</style>
